from flask import Flask, render_template, request
import db

app = Flask(__name__)


@app.route('/j')
def j_data():
    data_j = []
    with open('./static/j.txt', "rb") as j:
        for line in j:
            line = line[:-1]
            JNO, JNAME, CITY = line.decode("gbk").split("	")
            data_j.append((JNO, JNAME, CITY))
    return render_template("search.html", data_j=data_j)


@app.route('/', methods=['POST', 'GET'])
def main():
    if request.method == 'POST':
        SNO = request.values.get('SNO')
        SNAME = request.values.get('SNAME')
        STATUS = request.form.get('STATUS')
        CITY = request.values.get('CITY')
        db.insert_data_s(SNO, SNAME, STATUS, CITY)
    data_s = db.get_s_data()
    return render_template("main.html", data_s=data_s, method=request.method)


@app.route('/delete', methods=['POST', 'GET'])
def delete_s_data():
    if request.method == 'POST':
        delete = request.values.get('delete_data')
        db.delete_data_row(delete)
    data_s = db.get_s_data()
    return render_template("main.html", data_s=data_s, next=request.url)


@app.route('/spj', methods=['POST', 'GET'])
def update_spj():
    if request.method == 'POST':
        qty = request.values.get('qty')
        sno = request.values.get('sno')
        pno = request.values.get('pno')
        jno = request.values.get('jno')
        db.update_spj(sno,pno,jno,qty)
    data_spj = db.get_spj_data()
    return render_template("spj.html", data_spj=data_spj, next=request.url)


@app.route('/all')
def all():
    data_p = db.get_p_data()
    data_s = db.get_s_data()
    data_j = db.get_j_data()
    data_spj = db.get_spj_data()
    return render_template("all.html", data_s=data_s, data_p=data_p, data_spj=data_spj, data_j=data_j)

@app.route('/search', methods=['POST', 'GET'])
def search():
    if request.method == 'POST':
        sno = request.values.get('sno')
        db.search_s(sno)
    data_new = db.get_new_data()
    return render_template("search.html", data_new=data_new, next=request.url)



if __name__ == '__main__':
    app.run(host='127.0.0.1', port=5000, )
